<template>
  <div>
    <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <div>
        <!-- 搜索区域 -->
        <el-row :gutter="20">
          <el-col :span="8">
            <!-- 搜索框 -->
            <el-input
              v-model="queryInfo.query"
              placeholder="请输入搜索内容"
              clearable
            >
              <!-- 搜索按钮 -->
              <el-button slot="append" icon="el-icon-search"></el-button>
            </el-input>
          </el-col>
          <!-- 添加用户 -->
          <el-col :span="4">
            <el-button type="primary">添加商品</el-button>
          </el-col>
        </el-row>

        <!-- 列表区域 -->
        <el-table :data="goodslist" border style="width: 100%">
          <el-table-column type="index" label="#"> </el-table-column>
          <el-table-column prop="goods_name" label="商品名称" width="180">
          </el-table-column>
          <el-table-column prop="goods_price" label="商品价格" width="180">
          </el-table-column>
          <el-table-column prop="goods_weight" label="商品重量">
          </el-table-column>
          <el-table-column label="操作">
            <!-- 编辑按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
            ></el-button>
          </el-table-column>
        </el-table>

        <!-- 页码区域 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[1, 2, 5, 10]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
      goodslist: [],
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    //   当前页码改变时的数据
    handleCurrentChange(newPage) {
      console.log(newPage);
    },
    //   页码条数改变时的事件
    handleSizeChange(newSize) {
      console.log(newSize);
    },
    //   获取商品列表数据
    async getGoodsList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryInfo,
      });

      if (res.meta.status !== 200) {
        return this.$message.error("获取商品列表失败！");
      }

      this.$message.success("获取商品列表成功！");
      console.log(res.data);
      this.goodslist = res.data.goods;
      this.total = res.data.total;
    },
  },
};
</script>

<style scoped>
</style>